package com.pfzy.ui.activity.voice;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.pfzy.ui.utils.CommonUtil;

import java.io.File;

public class RecordAnimationView extends View {
    private Context mContext;
    private static final int MIN_RECORD_TIME = 1; // 最短录音时间，单位秒
    private static final int RECORD_OFF = 0; // 不在录音
    private static final int RECORD_ON = 1; // 正在录音
    private int recordState = 0; // 录音状态
//    private RecordStrategy mAudioRecorder;
    private Thread mRecordThread;
    private float recodeTime = 0.0f; // 录音时长，如果录音时间太短则录音失败
    private double voiceValue = 0.0; // 录音的音量值
    private boolean isCanceled = false; // 是否取消录音
    private Paint mPaint;
    private String path;
    // 录音线程
    private Runnable recordThread = new Runnable() {
        @Override
        public void run() {
            recodeTime = 0.0f;
            while (recordState == RECORD_ON) {
                try {
                    Thread.sleep(100);
                    recodeTime += 0.1;
                    // 获取音量，更新dialog
                    if (!isCanceled) {
                        voiceValue = AudioRecorder.getInstance().getMaxAmplitude();
                        _logd("更新 音量: " + voiceValue);
                        invalidate();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };

    public RecordAnimationView(Context context) {
        super(context);
        init(context);
    }

    public RecordAnimationView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    public RecordAnimationView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        mContext = context;
        mPaint = new Paint();
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.FILL);

//        this.mAudioRecorder = new AudioRecorder2();
    }

    public void voiceStart() {
        _logd("开始录音");
        if (recordState != RECORD_ON) {
            this.path = new File(getCacheDir(), System.currentTimeMillis() + ".aac").getAbsolutePath();
            AudioRecorder.getInstance().start(path);
//            if (mAudioRecorder != null) {
//                mAudioRecorder.ready();
                recordState = RECORD_ON;
//                mAudioRecorder.start();
                callRecordTimeThread();
//            }
        }
    }

    public void voiceStop() {
        _logd("停止录音");
        if (recordState == RECORD_ON) {
            recordState = RECORD_OFF;
//            if (mRecordDialog.isShowing()) {
//                mRecordDialog.dismiss();
//            }
            AudioRecorder.getInstance().stop();
            mRecordThread.interrupt();
            voiceValue = 0.0;
            if (isCanceled) {
//                AudioRecorder.getInstance().deleteOldFile();
            } else {
                if (recodeTime < MIN_RECORD_TIME) {
                    Log.d("录音", "时间太短  录音失败");
//                    AudioRecorder.getInstance().deleteOldFile();
                } else {
//                    if (listener != null) {
//                        listener.recordEnd(mAudioRecorder.getFilePath());
//                    }
                }
            }
            isCanceled = false;
        }
    }

    /**
     * 开启录音计时线程
     */
    private void callRecordTimeThread() {
        mRecordThread = new Thread(recordThread);
        mRecordThread.start();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        _logd("onDraw 音量: " + voiceValue);
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, (float) voiceValue, mPaint);
    }

//
//    public void setVolume(int volume) {
//        mVolume = volume;
//        invalidate();
//    }
//
//    private Dialog mRecordDialog;
//    private RecordButton.RecordListener listener;
//
//    private float downY;
//
//    private TextView dialogTextView;
//    private ImageView dialogImg;
//
//    public void setAudioRecord(RecordStrategy record) {
//        this.mAudioRecorder = record;
//    }
//
//    public void setRecordListener(RecordButton.RecordListener listener) {
//        this.listener = listener;
//    }
//
//    // 录音时显示Dialog
//    private void showVoiceDialog(int flag) {
//        if (mRecordDialog == null) {
//            mRecordDialog = new Dialog(mContext, R.style.Dialogstyle);
//            mRecordDialog.setContentView(R.layout.dialog_record);
//            dialogImg = (ImageView) mRecordDialog
//                    .findViewById(R.id.record_dialog_img);
//            dialogTextView = (TextView) mRecordDialog
//                    .findViewById(R.id.record_dialog_txt);
//        }
//        switch (flag) {
//            case 1:
//                dialogImg.setImageResource(R.drawable.record_cancel);
//                dialogTextView.setText("松开手指可取消录音");
//                this.setText("松开手指 取消录音");
//                break;
//
//            default:
//                dialogImg.setImageResource(R.drawable.record_animate_01);
//                dialogTextView.setText("向上滑动可取消录音");
//                this.setText("松开手指 完成录音");
//                break;
//        }
//        dialogTextView.setTextSize(14);
//        mRecordDialog.show();
//    }
//
//    // 录音时间太短时Toast显示
//    private void showWarnToast(String toastText) {
//        Toast toast = new Toast(mContext);
//        View warnView = LayoutInflater.from(mContext).inflate(
//                R.layout.toast_warn, null);
//        toast.setView(warnView);
//        toast.setGravity(Gravity.CENTER, 0, 0);// 起点位置为中间
//        toast.show();
//    }
//
//    // 录音Dialog图片随录音音量大小切换
//    private void setDialogImage() {
//        if (voiceValue < 600.0) {
//            dialogImg.setImageResource(R.drawable.record_animate_01);
//        } else if (voiceValue > 600.0 && voiceValue < 1000.0) {
//            dialogImg.setImageResource(R.drawable.record_animate_02);
//        } else if (voiceValue > 1000.0 && voiceValue < 1200.0) {
//            dialogImg.setImageResource(R.drawable.record_animate_03);
//        } else if (voiceValue > 1200.0 && voiceValue < 1400.0) {
//            dialogImg.setImageResource(R.drawable.record_animate_04);
//        } else if (voiceValue > 1400.0 && voiceValue < 1600.0) {
//            dialogImg.setImageResource(R.drawable.record_animate_05);
//        } else if (voiceValue > 1600.0 && voiceValue < 1800.0) {
//            dialogImg.setImageResource(R.drawable.record_animate_06);
//        } else if (voiceValue > 1800.0 && voiceValue < 2000.0) {
//            dialogImg.setImageResource(R.drawable.record_animate_07);
//        } else if (voiceValue > 2000.0 && voiceValue < 3000.0) {
//            dialogImg.setImageResource(R.drawable.record_animate_08);
//        } else if (voiceValue > 3000.0 && voiceValue < 4000.0) {
//            dialogImg.setImageResource(R.drawable.record_animate_09);
//        } else if (voiceValue > 4000.0 && voiceValue < 6000.0) {
//            dialogImg.setImageResource(R.drawable.record_animate_10);
//        } else if (voiceValue > 6000.0 && voiceValue < 8000.0) {
//            dialogImg.setImageResource(R.drawable.record_animate_11);
//        } else if (voiceValue > 8000.0 && voiceValue < 10000.0) {
//            dialogImg.setImageResource(R.drawable.record_animate_12);
//        } else if (voiceValue > 10000.0 && voiceValue < 12000.0) {
//            dialogImg.setImageResource(R.drawable.record_animate_13);
//        } else if (voiceValue > 12000.0) {
//            dialogImg.setImageResource(R.drawable.record_animate_14);
//        }
//    }
//
//    public interface RecordListener {
//        public void recordEnd(String filePath);
//    }

    public File getCacheDir() {
        File file = new File(CommonUtil.getContext().getCacheDir(), "im_cache");
        if (!file.exists()) {
            file.mkdirs();
        }
        return file;
    }

    private void _logd(String msg) {
        Log.d("RecordAnimationView", msg);
    }
}
